<template>
  <div class="app-container">

    <div class="Search">
      <!--工具条-->
      <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
        <el-form :inline="true" :model="query">
          <el-form-item>
            <el-input v-model="query.params.fiters" placeholder="过滤器" />
          </el-form-item>
          <el-form-item>
            <el-input v-model="query.params.predicates" placeholder="谓词" />
          </el-form-item>

          <el-form-item>
            <el-input v-model="query.params.uri" placeholder="URL" />
          </el-form-item>

          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="fetchData">查询</el-button>
          </el-form-item>
          <el-form-item>
            <el-button type="success" icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
          </el-form-item>
        </el-form>
      </el-col>

    </div>

    <div class="block">
      <el-table
        v-loading="listLoading"
        :data="list"
        element-loading-text="Loading"
        border
        fit
        highlight-current-row
      >
        <el-table-column align="center" label="序号" width="95">
          <template slot-scope="scope">
            {{ scope.$index }}
          </template>
        </el-table-column>
        <el-table-column label="过滤器" width="110">
          <template slot-scope="scope">
            {{ scope.row.fiters }}
          </template>
        </el-table-column>

        <el-table-column label="谓词" width="110">
          <template slot-scope="scope">
            {{ scope.row.predicates }}
          </template>
        </el-table-column>
        <el-table-column label="URL" width="110">
          <template slot-scope="scope">
            {{ scope.row.uri }}
          </template>
        </el-table-column>
        <el-table-column label="标注" width="110">
          <template slot-scope="scope">
            {{ scope.row.remark }}
          </template>
        </el-table-column>

        <el-table-column label="增加者" width="110">
          <template slot-scope="scope">
            {{ scope.row.createBy }}
          </template>
        </el-table-column>
        <el-table-column label="添加时间" width="110">
          <template slot-scope="scope">
            {{ scope.row.createDate }}
          </template>
        </el-table-column>
        <el-table-column label="更新者" width="110">
          <template slot-scope="scope">
            {{ scope.row.updateBy }}
          </template>
        </el-table-column>
        <el-table-column label="更新时间" width="110">
          <template slot-scope="scope">
            {{ scope.row.updateDate }}
          </template>
        </el-table-column>

        <el-table-column align="center" prop="created_at" label="操作" width="200">
          <template slot-scope="scope">
            <span class="el-tag el-tag--info el-tag--mini" style="cursor: pointer;" @click="handleEdit(scope.$index, scope.row)">
              {{ scope.row.isSet?'保存':"修改" }}
            </span>
            <span v-if="!scope.row.isSet" class="el-tag el-tag--danger el-tag--mini" style="cursor: pointer;" @click="handleDelete(scope.$index, scope.row)">
              删除
            </span>
            <span v-else class="el-tag  el-tag--mini" style="cursor: pointer;" @click="pwdChange(scope.row,scope.$index,false)">
              取消
            </span>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <div class="block">
      <el-pagination
        :current-page="currentPage"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>

    <div class="block">
      <!--弹框-->
      <el-dialog :title="titleMap[dialogStatus]" :visible.sync="dialogFormVisible">
        <el-form :model="form">

          <el-form-item v-if="addForVisible" label="主键">
            <el-input v-model="form.id" placeholder="" auto-complete="off" />
          </el-form-item>

          <el-form-item label="URL">
            <el-input v-model="form.uri" placeholder="URL" auto-complete="off" />
          </el-form-item>

          <el-form-item label="谓词">
            <el-input v-model="form.predicates" placeholder="谓词" auto-complete="off" />
          </el-form-item>

          <el-form-item label="过滤器">
            <el-input v-model="form.fiters" placeholder="过滤器" auto-complete="off" />
          </el-form-item>

          <el-form-item label="标注">
            <el-input v-model="form.remark" placeholder="" auto-complete="off" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="handleEditSave">确 定</el-button>
        </div>
      </el-dialog>
    </div>

  </div>
  </div>

</template>

<script>
import { getList, doDelete, doEdit } from '@/api/Route/'

export default {
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'gray',
        deleted: 'danger'
      }
      return statusMap[status]
    },
    typeFilter(status) {
      const typeMap = {
        '1': '超级管理员',
        '2': '管理员',
        '3': '普通用户'
      }
      const typeDesc = typeMap[status]
      return typeDesc
    }
  },
  data() {
    return {
      list: null,
      listLoading: true,
      total: 5,
      currentPage: 1,
      pageSize: 10,
      dialogFormVisible: false,
      addForVisible: false,
      form: {
        createBy: '',
        createDate: '',
        fiters: '',
        id: '',
        predicates: '',
        remark: '',
        updateBy: '',
        updateDate: '',
        uri: ''
      },
      // 新增or编辑弹框标题(根据点击的新增or编辑按钮显示不同的标题)
      titleMap: {
        add: '新增',
        edit: '编辑'
      },
      // 新增和编辑弹框标题
      dialogStatus: '',
      query: {
        params: {
          createBy: '', //
          createDate: '', //
          fiters: '', // 过滤器
          id: '', //
          predicates: '', // 谓词
          remark: '', //
          updateBy: '', //
          updateDate: '', //
          uri: '' // URL
        },
        timeValue: '',
        options: [{
          value: '1',
          label: '黄金糕'
        }, {
          value: '2',
          label: '双皮奶'
        }, {
          value: '3',
          label: '蚵仔煎'
        }, {
          value: '4',
          label: '龙须面'
        }, {
          value: '5',
          label: '北京烤鸭'
        }],
        value: ''
      }
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    fetchData() {
      this.listLoading = true
      getList(this.query.params, this.currentPage, this.pageSize).then(response => {
        const data = response.data
        this.total = data.totalCount
        this.pageSize = data.pageSize
        this.currentPage = data.currentPage
        this.list = data.entities
        this.listLoading = false
      })
    },
    handleSizeChange(val) {
      this.currentPage = 1
      this.pageSize = val
    },
    handleCurrentChange(val) {
      this.currentPage = val
    },
    // 新增
    handleAdd() {
      // 显示弹框
      this.dialogFormVisible = true
      // 新增弹框标题
      this.dialogStatus = 'add'

      this.form.createBy = ''
      this.form.createDate = ''
      this.form.fiters = ''
      this.form.id = ''
      this.form.predicates = ''
      this.form.remark = ''
      this.form.updateBy = ''
      this.form.updateDate = ''
      this.form.uri = ''
      this.addForVisible = false
    },
    // 编辑
    handleEdit(index, row) {
      console.log(index)
      console.log(row)

      this.form.createBy = row.createBy
      this.form.createDate = row.createDate
      this.form.fiters = row.fiters
      this.form.id = row.id
      this.form.predicates = row.predicates
      this.form.remark = row.remark
      this.form.updateBy = row.updateBy
      this.form.updateDate = row.updateDate
      this.form.uri = row.uri

      this.addForVisible = true

      // 显示弹框
      this.dialogFormVisible = true
      // 编辑弹框标题
      this.dialogStatus = 'edit'
    },
    handleDelete(index, row) {
      console.log(index)
      console.log(row)

      this.$confirm('此操作将永久删除该记录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
        center: true
      }).then(() => {
        doDelete(row.id).then(response => {
          const data = response.data
          console.log(data)
          if (data.status === 1) {
            this.$message({
              type: 'success',
              message: '操作成功!'
            })
            this.fetchData()
          } else if (data.status === 403) {
            this.$message({
              type: 'warning',
              message: data.description
            })
          } else {
            this.$message({
              type: 'info',
              message: '操作失败!'
            })
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    handleEditSave() {
      debugger

      doEdit(this.form).then(response => {
        debugger
        console.log(response)
        const data = response.data
        if (data.status) {
          this.$message({
            type: 'success',
            message: '操作成功!'
          })
          this.fetchData()
        } else {
          this.$message({
            type: 'info',
            message: '操作失败!'
          })
        }
        this.listLoading = false
        this.dialogFormVisible = false
      })
    },
    handleReset() {
      this.$confirm('此操作将永久删除该记录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
        center: true
      }).then(() => {
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    }
  }
}
</script>
